<script lang="ts" setup>
import type { Nullable } from '@vben/types';

import type { ScrollActionType } from '#/components/container';

import { ref, unref } from 'vue';

import { Page } from '@vben/common-ui';

import { Button, Card, Flex, Select, Skeleton } from 'ant-design-vue';

import {
  CollapseContainer,
  LazyContainer,
  ScrollContainer,
} from '#/components/container';
import {
  ExpandTransition,
  ExpandXTransition,
  FadeTransition,
  ScaleRotateTransition,
  ScaleTransition,
  ScrollXReverseTransition,
  ScrollXTransition,
  ScrollYReverseTransition,
  ScrollYTransition,
  SlideXReverseTransition,
  SlideXTransition,
  SlideYReverseTransition,
  SlideYTransition,
} from '#/components/transition';

import TargetContent from './modules/target-content.vue';

defineOptions({
  name: 'CompScroll',
});

const scrollRef = ref<Nullable<ScrollActionType>>(null);
const getScroll = () => {
  const scroll = unref(scrollRef);
  if (!scroll) {
    throw new Error('scroll is Null');
  }
  return scroll;
};
function scrollTo(top: number) {
  getScroll().scrollTo(top);
}
function scrollBottom() {
  getScroll().scrollBottom();
}

const transitionList = [
  'Fade',
  'Scale',
  'SlideY',
  'ScrollY',
  'SlideYReverse',
  'ScrollYReverse',
  'SlideX',
  'ScrollX',
  'SlideXReverse',
  'ScrollXReverse',
  'ScaleRotate',
  'ExpandX',
  'Expand',
];
const options = transitionList.map((item) => ({
  label: item,
  value: item,
  key: item,
}));
const value = ref('Fade');
const show = ref(true);
function start() {
  show.value = false;
  setTimeout(() => {
    show.value = true;
  }, 500);
}
</script>

<template>
  <Page auto-content-height>
    <Flex horizontal>
      <Card title="滚动" style="width: 50%">
        <div class="my-4">
          <Button class="mr-2" @click="scrollTo(100)">滚动到100px位置</Button>
          <Button class="mr-2" @click="scrollTo(800)">滚动到800px位置</Button>
          <Button class="mr-2" @click="scrollTo(0)">滚动到顶部</Button>
          <Button class="mr-2" @click="scrollBottom()">滚动到底部</Button>
        </div>
        <div class="scroll-wrap">
          <ScrollContainer ref="scrollRef" class="mt-4">
            <ul class="p-3">
              <template v-for="index in 100" :key="index">
                <li class="p-2" :style="{ border: '1px solid #eee' }">
                  {{ index }}
                </li>
              </template>
            </ul>
          </ScrollContainer>
        </div>
      </Card>
      <Card title="虚拟滚动" style="width: 50%" class="ml-2" />
    </Flex>
    <Flex horizontal class="mt-2">
      <Card title="动画组件示例" style="width: 50%; height: 300px">
        <div class="flex">
          <Select
            :options="options"
            v-model:value="value"
            placeholder="选择动画"
            :style="{ width: '150px' }"
          />
          <a-button type="primary" class="ml-4" @click="start">
            start
          </a-button>
        </div>
        <ScaleTransition v-if="value === 'Scale'">
          <div class="box" v-show="show"></div>
        </ScaleTransition>
        <SlideYTransition v-else-if="value === 'SlideY'">
          <div class="box" v-show="show"></div>
        </SlideYTransition>
        <ScrollYTransition v-else-if="value === 'ScrollY'">
          <div class="box" v-show="show"></div>
        </ScrollYTransition>
        <SlideYReverseTransition v-else-if="value === 'SlideYReverse'">
          <div class="box" v-show="show"></div>
        </SlideYReverseTransition>
        <ScrollYReverseTransition v-else-if="value === 'ScrollYReverse'">
          <div class="box" v-show="show"></div>
        </ScrollYReverseTransition>
        <SlideXTransition v-else-if="value === 'SlideX'">
          <div class="box" v-show="show"></div>
        </SlideXTransition>
        <ScrollXTransition v-else-if="value === 'ScrollX'">
          <div class="box" v-show="show"></div>
        </ScrollXTransition>
        <ExpandXTransition v-else-if="value === 'ExpandX'">
          <div class="box" v-show="show"></div>
        </ExpandXTransition>

        <SlideXReverseTransition v-else-if="value === 'SlideXReverse'">
          <div class="box" v-show="show"></div>
        </SlideXReverseTransition>
        <ScrollXReverseTransition v-else-if="value === 'ScrollXReverse'">
          <div class="box" v-show="show"></div>
        </ScrollXReverseTransition>
        <ScaleRotateTransition v-else-if="value === 'ScaleRotate'">
          <div class="box" v-show="show"></div>
        </ScaleRotateTransition>

        <ExpandTransition v-else-if="value === 'Expand'">
          <div class="box" v-show="show"></div>
        </ExpandTransition>
        <FadeTransition v-else>
          <div class="box" v-show="show"></div>
        </FadeTransition>
      </Card>
      <Card
        title="折叠容器"
        class="ml-2"
        :style="{ 'background-color': 'gray', width: '50%' }"
      >
        <CollapseContainer title="员工信息"> 我是里面的内容 </CollapseContainer>
      </Card>
    </Flex>
    <Flex horizontal class="mt-2">
      <Card title="懒加载基础示例" style="width: 50%">
        <div class="lazy-base-demo-wrap">
          <h1>向下滚动</h1>

          <div class="lazy-base-demo-box">
            <LazyContainer :max-waiting-time="2000">
              <TargetContent />
              <template #skeleton>
                <Skeleton :paragraph="{ rows: 10 }" active :loading="true" />
              </template>
            </LazyContainer>
          </div>
        </div>
      </Card>
      <Card
        title="向下滚动（懒加载自定义动画示例）"
        style="width: 50%"
        class="ml-2"
      >
        <div class="lazy-base-demo-wrap">
          <h1>向下滚动</h1>

          <div class="lazy-base-demo-box">
            <LazyContainer transition-name="custom" :max-waiting-time="2000">
              <TargetContent />
            </LazyContainer>
          </div>
        </div>
      </Card>
    </Flex>
  </Page>
</template>

<style lang="scss" scoped>
.scroll-wrap {
  height: 300px;
  background-color: #f0f2f5;
}

.box {
  width: 100px;
  height: 100px;
  margin-top: 20px;
  background-color: rgb(126 170 236);
}

.lazy-base-demo {
  &-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 50%;
    height: 2000px;
    margin: 20px auto;
    text-align: center;
  }

  &-box {
    width: 300px;
    height: 300px;
  }

  h1 {
    height: 1300px;
    margin: 20px 0;
  }
}
</style>
